/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author johnkim
 *
 */
public class DayveMemberDatabaseHandler {

	public static final String TAG = DayveMemberDatabaseHandler.class.getSimpleName();
	
	Context ctx;
	DayveMemberDatabaseHelper helper;
	SQLiteDatabase db;
	
	public DayveMemberDatabaseHandler(Context ctx) {
		this.ctx = ctx;
		
		helper = new DayveMemberDatabaseHelper(ctx);		
		
		db = helper.getWritableDatabase();
	}
	
	/**
	 * Database open 
	 */	
    public static DayveMemberDatabaseHandler open(Context ctx) {
    	return new DayveMemberDatabaseHandler(ctx);
    }
    
    /**
     * Transaction 을 정의 한다.
     */
	public void beginTransaction() {db.beginTransaction();}
	
	public void setTransactionSuccessful() {db.setTransactionSuccessful();}
	
	public void endTransaction() {db.endTransaction();}

	public SQLiteDatabase getSQLiteDatabase() {
		return this.db;
	}
	
	/**
	 * Database close 
	 * */
	public void close() {
		if(db.isOpen()) db.close();
	}
	
	/**
	 * Member table 컬럼 목록.
	 */
	private static final String[] DAYVE_MEMBER = new String[] {
		"_id", 
		"id",
		"password",
		"email",
		"open_email_yn",
		"mobile_phone_number",
		"open_mobile_phone_yn",
		"country",
		"open_country_yn",
		"home_page",
		"open_home_page_yn",
		"nick_name",
		"open_nick_name_yn",
		"profile_image_url",
		"open_profile_image_yn",
		"facebook_id",
		"facebook_password",
		"open_facebook_yn",
		"twitter_id",
		"twitter_password",
		"open_twitter_yn",
		"birthday",
		"sex",
		"mileage",
		"level",
		"activate_yn",
		"modify_date", 
		"register_date"
	};
	
	public ArrayList<DayveMember> selectMemberList(String _id, String limit) {
		Log.i(TAG, "selectMemberList");
		
		ArrayList<DayveMember> memberList = new ArrayList<DayveMember>();
		
		Cursor cursor = db.query(
				"dayve_member", 
				DAYVE_MEMBER, 
				"_id>?", 
				new String[] {_id}, 
				null, 
				null, 
				" _id ASC ", 
				limit
				);
		
		DayveMember dayveMember = null;
		
		while(cursor.moveToNext()) {
			dayveMember = new DayveMember();
			
			dayveMember.set_id(cursor.getString(0));
			dayveMember.setId(cursor.getString(1));
			dayveMember.setPassword(cursor.getString(2));
			dayveMember.setEmail(cursor.getString(3));
			dayveMember.setOpenEmailYn(cursor.getString(4));
			dayveMember.setMobilePhoneNumber(cursor.getString(5));
			dayveMember.setOpenMobilePhoneYn(cursor.getString(6));
			dayveMember.setCountry(cursor.getString(7));
			dayveMember.setOpenCountryYn(cursor.getString(8));
			dayveMember.setHomePage(cursor.getString(9));
			dayveMember.setOpenHomePageYn(cursor.getString(10));
			dayveMember.setNickName(cursor.getString(11));
			dayveMember.setOpenNickNameYn(cursor.getString(12));
			dayveMember.setProfileImageUrl(cursor.getString(13));
			dayveMember.setOpenProfileImageYn(cursor.getString(14));
			dayveMember.setFacebookId(cursor.getString(15));
			dayveMember.setFacebookPassword(cursor.getString(16));
			dayveMember.setOpenFacebookYn(cursor.getString(17));
			dayveMember.setTwitterId(cursor.getString(18));
			dayveMember.setTwitterPassword(cursor.getString(19));
			dayveMember.setOpenTwitterYn(cursor.getString(20));
			dayveMember.setBirthday(cursor.getString(21));
			dayveMember.setSex(cursor.getString(22));
			dayveMember.setMileage(cursor.getString(23));
			dayveMember.setLevel(cursor.getString(24));
			dayveMember.setActivateYn(cursor.getString(25));
			dayveMember.setModifyDate(cursor.getString(26));
			dayveMember.setRegisterDate(cursor.getString(27));
			
			memberList.add(dayveMember);
		}
		
		cursor.close();
		
		return memberList;
	}
	
	/**
	 * Member 정보를 가져 온다.
	 * 
	 * @param memberId
	 * @return
	 */
	public DayveMember selectMember(String memberId) {
		Log.i(TAG, "selectMember");
		
		DayveMember dayveMember = new DayveMember();
		
		Cursor cursor = db.query(
				"dayve_member", 				// 테이블.
				DAYVE_MEMBER, 					// 컬럼.
				"id = ?", 						// 조건문.
				new String[] {memberId}, 		// 조건값.
				null, 
				null, 
				null
				);
		
		/**
		 * 현재, 페이징 처리와 한번에 가져 오는 로직이 섞여 있어서 
		 * 같은 데이터들이 중복으로 들어 있기때문에 같은 데이터의 첫번째 결과만 
		 * 가져 오도록 cursor.moveToFirst() 정의 함.
		 * 
		 * 추후 한번에 가져 오는 로직으로 모두 수정될 경우 삭제 하면 된다.
		 */
		cursor.moveToFirst();
		
		dayveMember.set_id(cursor.getString(0));
		dayveMember.setId(cursor.getString(1));
		dayveMember.setPassword(cursor.getString(2));
		dayveMember.setEmail(cursor.getString(3));
		dayveMember.setOpenEmailYn(cursor.getString(4));
		dayveMember.setMobilePhoneNumber(cursor.getString(5));
		dayveMember.setOpenMobilePhoneYn(cursor.getString(6));
		dayveMember.setCountry(cursor.getString(7));
		dayveMember.setOpenCountryYn(cursor.getString(8));
		dayveMember.setHomePage(cursor.getString(9));
		dayveMember.setOpenHomePageYn(cursor.getString(10));
		dayveMember.setNickName(cursor.getString(11));
		dayveMember.setOpenNickNameYn(cursor.getString(12));
		dayveMember.setProfileImageUrl(cursor.getString(13));
		dayveMember.setOpenProfileImageYn(cursor.getString(14));
		dayveMember.setFacebookId(cursor.getString(15));
		dayveMember.setFacebookPassword(cursor.getString(16));
		dayveMember.setOpenFacebookYn(cursor.getString(17));
		dayveMember.setTwitterId(cursor.getString(18));
		dayveMember.setTwitterPassword(cursor.getString(19));
		dayveMember.setOpenTwitterYn(cursor.getString(20));
		dayveMember.setBirthday(cursor.getString(21));
		dayveMember.setSex(cursor.getString(22));
		dayveMember.setMileage(cursor.getString(23));
		dayveMember.setLevel(cursor.getString(24));
		dayveMember.setActivateYn(cursor.getString(25));
		dayveMember.setModifyDate(cursor.getString(26));
		dayveMember.setRegisterDate(cursor.getString(27));
		
		cursor.close();
		
		return dayveMember;
	}
	
	/**
	 * 그룹을 저장 한다.
	 * 
	 * @param values
	 * @return
	 */
	public Long insertDayveMember(ContentValues values) {
//		Log.i(TAG, "insertDayveGroup");
		return  db.insert("dayve_member", "", values);
	}
	
	/**
	 * 그룹을 삭제 한다.
	 * 
	 * @return
	 */
	public int deleteDayveMember() {
		Log.i(TAG, "deleteDayveMember");
		return db.delete("dayve_member", null, null);
	}
}
